package com.hwy.catering.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwy.catering.dto.MealDto;
import com.hwy.catering.entity.Category;
import com.hwy.catering.entity.Dish;
import com.hwy.catering.entity.Meal;
import com.hwy.catering.entity.MealDetail;
import com.hwy.catering.mapper.CategoryMapper;
import com.hwy.catering.mapper.DishMapper;
import com.hwy.catering.mapper.MealDetailMapper;
import com.hwy.catering.mapper.MealMapper;
import com.hwy.catering.service.MealDetailService;
import com.hwy.catering.service.MealService;
import com.hwy.catering.utils.R;
import org.redisson.misc.Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MealServiceImpl extends ServiceImpl<MealMapper, Meal> implements MealService {

    @Resource
    private MealMapper mealMapper;

    @Resource
    private MealDetailMapper mealDetailMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    @Override
    @Transactional
    public R deleteOneMeal(Long id) {
        mealMapper.deleteById(id);
        LambdaQueryWrapper<MealDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MealDetail::getMealId,id);
        mealDetailMapper.delete(wrapper);
        return R.success("成功删除套餐。");
    }

    @Override
    @Transactional
    public R editMeal(MealDto mealDto) {
        Meal meal = new Meal();
        BeanUtil.copyProperties(mealDto,meal);
        //更新meal表
        mealMapper.updateById(meal);
        //更新mealDeatil表,策略：先删除和该类别有管的所有菜品，然后再重新添加
        //先删除
        LambdaQueryWrapper<MealDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MealDetail::getMealId,meal.getId());
        mealDetailMapper.delete(wrapper);
        //后更改
        List<List<String>> dishList = mealDto.getDishList();
        for (List<String> dishId :dishList) {
            MealDetail mealDetail = new MealDetail();
            //获取插入套餐生成的自增id,存入
            mealDetail.setMealId(meal.getId());
            //取出菜品id,存入
            mealDetail.setDishId(Long.valueOf(dishId.get(1)));
            mealDetailMapper.insert(mealDetail);
        }
        return R.success("套餐更改成功。");
    }

    @Override
    public R getMealById(Long id) {
        MealDto mealDto = new MealDto();
        Meal meal = mealMapper.selectById(id);
        BeanUtil.copyProperties(meal,mealDto);
        //还差个List<List<Long>> dishList，格式：[[类别id,菜品id],[类别id,菜品id],[类别id,菜品id]]
        ArrayList<List<String>> arrayList = new ArrayList<>();
        LambdaQueryWrapper<MealDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MealDetail::getMealId,meal.getId());
        List<MealDetail> mealDetails = mealDetailMapper.selectList(wrapper);
        for (MealDetail mealDetail:mealDetails) {
            Dish dish = dishMapper.selectById(mealDetail.getDishId());
            ArrayList<String> list = new ArrayList<>();
            list.add(dish.getCategoryId().toString());
            list.add(dish.getId().toString());
            //此时 [类别id,菜品id] 已经准备好了
            arrayList.add(list);
        }
        mealDto.setDishList(arrayList);
        return R.success(mealDto);
    }

    @Override
    public R findMealAll(Page<Meal> pageParam,Meal meal) {
        QueryWrapper<Meal> wrapper1 = new QueryWrapper<>();
        if ( meal.getName() != null && !meal.getName().trim().isEmpty()) {
            wrapper1.eq("name",meal.getName());
        } else if (meal.getCategoryId() != null ) {
            wrapper1.eq("category_id",meal.getCategoryId());
        }
        HashMap<Object, Object> finalMap = new HashMap<>();
        ArrayList<MealDto> finalList = new ArrayList<>();
        Page<Meal> page = page(pageParam, wrapper1);
        List<Meal> meals = page.getRecords();
//        List<Meal> meals = mealMapper.selectList(wrapper1);
        ArrayList<Object> list1 = new ArrayList<>();
        for (Meal meal1 :meals) {
            MealDto mealDto = new MealDto();
            //获取类别名
            Category category = categoryMapper.selectById(meal1.getCategoryId());
            mealDto.setCategoryName(category.getName());
            BeanUtil.copyProperties(meal1,mealDto);
            finalList.add(mealDto);
            //获取dishListName数据
            LambdaQueryWrapper<MealDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MealDetail::getMealId,meal1.getId());
            List<MealDetail> mealDetails = mealDetailMapper.selectList(wrapper);
            ArrayList<Object> list = new ArrayList<>();
            for (MealDetail mealDetail :mealDetails) {
                HashMap<Object, Object> hashMap = new HashMap<>();
                Dish dish = dishMapper.selectById(mealDetail.getDishId());
                hashMap.put("name",dish.getName());
                list.add(hashMap);
            }
            list1.add(list);
        }
        finalMap.put("page",page);
        finalMap.put("finalList",finalList);
        finalMap.put("dishListName",list1);
        return R.success(finalMap);
    }

    @Override
    @Transactional
    public R addMeal(MealDto mealDto) {
        Meal meal = new Meal();
        meal.setCategoryId(mealDto.getCategoryId());
        meal.setName(mealDto.getName());
        meal.setPrice(mealDto.getPrice());
        meal.setSort(mealDto.getSort());
        meal.setState(mealDto.getState());
        mealMapper.insert(meal);
        List<List<String>> dishList = mealDto.getDishList();
        for (List<String> dishId :dishList) {
            MealDetail mealDetail = new MealDetail();
            //获取插入套餐生成的自增id,存入
            mealDetail.setMealId(meal.getId());
            //取出菜品id,存入
            mealDetail.setDishId(Long.valueOf(dishId.get(1)));
            int insert = mealDetailMapper.insert(mealDetail);
            System.out.println(insert);
        }
        return R.success("添加成功。");
    }
}
